package components.photo
{
	import UIFree.ObjectEvent;
	import UIFree.uif_Timer;
	import UIFree.uif_component;
	
	import UIPhoto.uip_photoEditor;
	import UIPhoto.uip_rect;
	
	import classes.clt_events;
	import classes.clt_loader;
	
	import components.clt_workspace;
	
	import data.cd_masks;
	import data.cd_utils;
	import data.clt_data;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import jp.maaash.ObjectDetection.ObjectDetector;
	import jp.maaash.ObjectDetection.ObjectDetectorEvent;
	import jp.maaash.ObjectDetection.ObjectDetectorOptions;

	public class clt_photoEditor extends uip_photoEditor {
		public static const MAXFACEDETECTWAIT: Number = 2000; // 2-сек
		
		private var _maskID			: int;
		private var _maskLoader		: clt_loader
		public var _restoreInfo		: Object;
		private var _noFaceDetect	: Boolean;
		
		public function clt_photoEditor(parent:uif_component, images:Object, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, styleClass:String='') {
			super(parent, images, ax, ay, awidth, aheight, styleClass);
			_noFaceDetect = false;
		}
		
		override protected function listenEvents():void {
			super.listenEvents();
			addEvent(clt_events.GETHEAD, doGetHead, workspace);
			addEvent(clt_events.SETHEAD, doSetHead, workspace);
			addEvent(clt_events.GETPHOTO, doGetPhoto, workspace);
			addEvent(clt_events.SETPHOTORECT, doSetPhotoRect, workspace);
		}
		
		private function doGetPhoto(e: ObjectEvent): void {
			e.object.result = photo;
		}
		
		private function doSetPhotoRect(e: Object): void {
			rect.reset(null, _restoreInfo);
			workspace.dispatchEvent(new Event(clt_events.UPDATECUTOUT));
		}
		
		override protected function createRect(images:Object):uip_rect {
			return new clt_rect(this, images);
		}
		
		protected function doGetHead(e: ObjectEvent): void {
			e.object.maskID 		= _maskID; 
			e.object.restoreInfo 	= rect.getRestoreInfo();
			e.object.bmp			= clipBitmap;
		}
		
		protected function doSetHead(e: ObjectEvent): void {
			
			var resData: Object = e.object;
			_restoreInfo 		= resData.restoreInfo;
			workspace.maskInfo  = cd_utils.fromID(cd_masks.list, resData.maskID);
			workspace.loadPhoto(resData.image);
/*
			new uif_Timer(5000, null, function(e: Event): void {
				_restoreInfo = null;
			}, 1);
*/			
		}
		
		override public function resetPhoto(a_photo:Bitmap=null):void {
			super.resetPhoto(a_photo);
			if (!_restoreInfo) _restoreInfo = faceDetect();
			
			new uif_Timer(2000, null, function(e: Event): void {
				_restoreInfo = null;
			}, 1);
		}
		
		protected function updateMaskBitmap(a_mask: DisplayObject): void {
//			if (!_restoreInfo) _restoreInfo = faceDetect();
			rect.reset(new Point(a_mask.width, a_mask.height), _restoreInfo);
			(rect as clt_rect).assignMask(a_mask);
			workspace.dispatchEvent(new Event(clt_events.UPDATECUTOUT));
		}
		
		protected function get workspace(): clt_workspace {
			return (app as clt_clothingBase).workspace;
		}
		
		public function refreshMask(): void {
			if (workspace.maskInfo) {
				if (_maskID != workspace.maskInfo.id) {
					if (_maskLoader) _maskLoader.unload();
					_maskID = workspace.maskInfo.id;
					new clt_loader(null, clt_servers.MASK_URL + _maskID + clt_data.JPGEXT, function(e: Event): void {
						_maskLoader = e.target.loader;
						updateMaskBitmap(_maskLoader);
					});
				} else if (_maskLoader) updateMaskBitmap(_maskLoader);
			}
		}
		
		public function get clipBitmap(): BitmapData {
			return (rect as clt_rect).clipBitmap;
		}
		
		protected function faceDetect(): Object {
			if (_noFaceDetect) return null;
			
			function ehp_AsFace(): Rectangle {
				var od: ObjectDetector = new ObjectDetector();
				var rect: Rectangle = null;
				var time: Number = (new Date()).time;
				od.cascade.base_window_h = 25;
				
				
				od.addEventListener(ObjectDetectorEvent.DETECTION_COMPLETE, function(e: ObjectDetectorEvent): void {
					var max: Rectangle;
					
					for (var i:int=0; i<e.rects.length; i++) {
						var r: Rectangle = e.rects[i] as Rectangle;
						if (!max || (r.size.length > max.size.length)) {
							max = r.clone();
						}
					}				
					rect = max;
					_noFaceDetect = (new Date()).time - time > MAXFACEDETECTWAIT;
				});
				
				var op: ObjectDetectorOptions =  new ObjectDetectorOptions();
				op.min_size = 70;
				op.scale_factor = 1.2;
				od.options = op;
				
				od.detect(photo.bitmapData);
				return rect;
			}
			var _faceRect: Rectangle = ehp_AsFace();//super.EnterHerePhoto();
			if (_faceRect) {
				var rect	: Rectangle = EnterHerePhoto();
				var scale	: Number 	= rect.width / photo.bitmapData.width;
				var w		: Number	= _faceRect.width * scale;
				
				rect = new Rectangle(rect.x + _faceRect.x * scale, rect.y + _faceRect.y * scale, w, w);
				rect.y -= rect.height * 0.3;
				rect.inflate(rect.width * 0.22, rect.height * 0.22);
				return rect;
			}
			return null;
		}
		
		override protected function doSetNewPhoto(a_photo:Bitmap):void {
			super.doSetNewPhoto(a_photo);
			if (!_restoreInfo) _restoreInfo = faceDetect();
		}
	}
}